﻿//
// Program.cs: Application entry point.
//
// Copyright 2010 Michael Goethe
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using S9PalCommon;

namespace S9Pal
{
    class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
#if (!DEBUG)
            try
            {
#endif
                // Only allow one instance of the application to run at once.
                Mutex mutex;
                bool notAlreadyRunning;
                
                mutex = new Mutex(true, "S9Pal", out notAlreadyRunning);

                if (notAlreadyRunning)
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new MainForm());

                    // Prevent the mutex from being garbage collected.
                    GC.KeepAlive(mutex);
                }
#if (!DEBUG)
            }
            catch (Exception ex)
            {
                S9PalCommon.Global.Logger.Fatal(ex.ToString());
                MessageBox.Show(ex.Message.ToString(), ex.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw ex;
            }
#endif
        }
    }
}
